#include "apue.h"
#include <pthread.h>

pthread_t ntid;

void
printids(const char *s)
{
	pid_t		pid;
	pthread_t	tid;

	//获取进程id
	pid = getpid();

	//获取线程id
	tid = pthread_self();
	printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid,
	  (unsigned long)tid, (unsigned long)tid);
}

/**
* 线程要执行的任务
* 打印新线程信息
*/
void *
thr_fn(void *arg)
{
	printids("new thread: ");
	return((void *)0);
}

int
main(void)
{
	int		err;

	//创建线程，线程入口函数为thr_fn
	err = pthread_create(&ntid, NULL, thr_fn, NULL);
	if (err != 0)
		err_exit(err, "can't create thread");

	//打印主线程信息
	printids("main thread:");
	sleep(1);
	exit(0);
}


/*Linux下执行
[root@localhost threads]# ./threadid 
main thread: pid 8035 tid 139752673752896 (0x7f1ab479b740)
new thread:  pid 8035 tid 139752663254784 (0x7f1ab3d98700)
*/
